// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Game from "./Game";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Player extends cc.Component {

    @property
    minX: number = 0;

    @property
    maxX: number = 0;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

        this.node.on('touchmove', this.onTouchMove, this);
    }

    start () {

        //创建定时器
        //this.schedule(this.onTimer, 0.3, cc.macro.REPEAT_FOREVER, 0.01);
    }

    // 发射子弹
    shoot() {

        if(Game.inst.playing == false) return;

        // 动态创建子弹
        let bulletNode = Game.inst.createBullet();

        bulletNode.setPosition(this.node.x, this.node.y + this.node.height);
        this.node.parent.addChild(bulletNode); //挂在Canvas节点下面
    }

    onTouchMove(e: cc.Event.EventTouch) {

        let delta = e.getDelta();

        //范围检测
        if(this.node.x < this.minX) this.node.x = this.minX;
        if(this.node.x > this.maxX) this.node.x = this.maxX;

        if(this.node.x <= this.maxX && this.node.x >= this.minX) {
            
            this.node.x += delta.x;
        }       
    }

    // update (dt) {}

    onCollisionEnter(other: cc.Collider, self: cc.Collider) {

        Game.inst.gameOver();
    }

    /*
    onTimer() {

        if(Game.inst.playing == false) return;

        // 动态创建子弹
        let bulletNode = Game.inst.createBullet();

        bulletNode.setPosition(this.node.x, this.node.y + this.node.height);
        this.node.parent.addChild(bulletNode); //挂在Canvas节点下面
    }*/
}
